package com.define.ssh.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.define.ssh.common.JqueryZTreeNode;
import com.define.ssh.dao.CompdeptDao;
import com.define.ssh.model.Compdept;
import com.define.ssh.service.CompdeptService;

@Service("compdeptServiceImpl")
public class CompdeptServiceImpl extends BaseServiceImpl<Compdept, Integer>
		implements CompdeptService {

	@Resource(name = "compdeptDaoImpl")
	private CompdeptDao compdeptDao;

	@Resource(name = "compdeptDaoImpl")
	public void setBaseDao(CompdeptDao compdeptDao) {
		super.setBaseDao(compdeptDao);
	}

	/***
	 * 加载tree
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public List<JqueryZTreeNode> loadTree(Integer pid) {

		final HttpServletRequest request = ServletActionContext.getRequest();
		final String ctx = request.getContextPath();
		final List<JqueryZTreeNode> treeList = new ArrayList<JqueryZTreeNode>();

		// 通过pid 查询
		final DetachedCriteria dc = DetachedCriteria.forClass(Compdept.class);
		dc.add(Restrictions.eq("pid", pid));
		dc.addOrder(Order.asc("sortmanager"));
		final List<Compdept> list = compdeptDao.findByCriteria(dc);

		if (list.size() > 0) {
			for (final Compdept compdept : list) {

				final JqueryZTreeNode tree = new JqueryZTreeNode();
				tree.setName(compdept.getDeptname());
				tree.setId(String.valueOf(compdept.getId()));
				// tree.setType(String.valueOf(columnManage.getMcmType()));
				if (compdept.getPid() != null) {
					tree.setpId(String.valueOf(compdept.getPid()));
				}
				tree.setOpen(true);
				tree.setChecked(false);
				// tree.setTemp1(area.getAreaType());
				// 判断当前节点是否有子节点
				/*
				 * DetachedCriteria
				 * dc1=DetachedCriteria.forClass(Compdept.class);
				 * dc1.add(Restrictions.eq("pid",compdept.getId()));
				 * List<Compdept> list1=compdeptDao.findByCriteria(dc1)
				 */;

				// BigInteger count= compdeptDao.getDeptCount(compdept.getId());

				if (compdept.getPid().equals("0")) {

					// if(count!=null&&Integer.valueOf(count.toString())>0){
					tree.setIsParent("true");

					// tree.setIconOpen(ctx+JqueryZTreeNode.iconOpenText);
					// tree.setIconClose(ctx+JqueryZTreeNode.iconCloseText);
				} else {
					tree.setIsParent("false");
					// tree.setIcon(ctx+JqueryZTreeNode.iconText);
					// tree.setIcon(ctx+"/scripts/zTree/zTreeStyle/img/join.gif");
				}
				treeList.add(tree);
			}

		}
		return treeList;

	}
	

	@Override
	public int getTotalByDeptId(int id) {
	    return this.compdeptDao.getTotalByDeptId(id);
	}
	
	public void delete1(int id){
		this.compdeptDao.delete1(id);
	}
	
	@Override
	public Integer count(Integer pid) {
		return compdeptDao.getDeptCount(pid);
	}
}
